package com.cw.jeeyt.controller.git;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.git.GitBranchBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.git.GitBranchService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * git项目分支信息控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/gitBranch")
public class GitBranchController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(GitBranchController.class);
    @Resource
    private GitBranchService gitBranchService;

    /**
     * 跳转到git项目分支信息列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {

        ModelAndView mv = new ModelAndView("git/gitBranch_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listGitBranch")
    public ResultData listGitBranch(@RequestBody DataTable dataTable) throws Exception {

        GitBranchBo gitBranchBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            GitBranchBo.class);
        if (gitBranchBo == null) {
            gitBranchBo = new GitBranchBo();
        }
        PageInfo<GitBranchBo> pageInfo = gitBranchService.selectPage(gitBranchBo, PageParam.Builder.getInstance()
            .addPageNum(PageUtil.calPageNum(dataTable.getStart(), dataTable.getLength()))
            .addPageSize(dataTable.getLength())
            .build());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到git项目分支信息表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(Long id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("git/gitBranch_form");
        GitBranchBo gitBranchBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            gitBranchBo = new GitBranchBo();
        } else {
            gitBranchBo = gitBranchService.selectByKey(id);
        }
        mv.addObject("gitBranch", gitBranchBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到git项目导入sql脚本页面
     *
     * @param projectId git项目编号
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-sqlImportForm")
    public ModelAndView editForm(Long projectId) throws Exception {

        ModelAndView mv = new ModelAndView("git/gitProject_sqlImportForm");
        mv.addObject("projectId", projectId);
        return mv;
    }

    /**
     * 根据操作类型保存git项目分支信息数据
     *
     * @param response
     * @param gitBranchBo
     * @param operation add 新增  edit 修改
     */
    @ResponseBody
    @RequestMapping(value = "/saveGitBranch", method = RequestMethod.POST)
    public JsonResultDto saveGitBranch(GitBranchBo gitBranchBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                gitBranchService.addGitBranch(gitBranchBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                gitBranchService.modifyGitBranch(gitBranchBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存git项目分支信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除git项目分支信息
     *
     * @param gitBranchBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(GitBranchBo gitBranchBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            gitBranchService.deleteGitBranch(gitBranchBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除git项目分支信息失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 导入sql脚本
     *
     * @param request
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/importSqlScript", method = RequestMethod.POST)
    public JsonResultDto importSqlScript(HttpServletRequest request) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            //gitBranchService.importSqlScript(request);
            jsonResultDto.setMsg("导入脚本成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("导入脚本失败：" + e.getMessage());
            logger.error("导入脚本失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
